package com.etcn.modules.statical.statistics.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.etcn.common.config.InterfaceConfig;
import com.etcn.common.constants.Constants;
import com.etcn.common.utils.PubFun;
import com.etcn.common.utils.StringUtil;
import com.etcn.common.utils.secret.MD5Util;
import com.etcn.modules.statical.statistics.service.IExperienceService;

/**
 * 归类经验相关统计
 * 
 * @author guozc
 * @date 2018年6月18日 上午6:48:15
 */
@Service("experienceService")
public class ExperienceServiceImpl implements IExperienceService {

    @Autowired
    private InterfaceConfig interfaceConfig;

    @Value("${experiences.req.usercode}")
    private String reqUserCode;

    @Value("${experiences.req.encodestr}")
    private String encodestr;

    @Value("${server.ip}")
    private String serverIp;

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> categoryDataword(
            String params) throws Exception {
        Map<String, Object> ret = new HashMap<String, Object>();

        // 解析参数
        JSONObject obj = JSONObject.parseObject(params);
        String querykey = obj.getString("querykey");
        if (StringUtil.isEmpty(querykey)) {
            PubFun.renderErrorMsg("查询条件不能为空");
        }
        String ishs = null;
        Pattern pattern = Pattern.compile("^(\\d{4}|\\d{6}|\\d{8}|\\d{10})$");
        Matcher matcher = pattern.matcher(querykey);
        if (matcher.matches()) {
            ishs = "1";
        } else {
            ishs = "0";
        }
        Integer start = obj.getInteger("start");
        if (start == null) {
            start = 0;
        }

        String usertoken = MD5Util.Md5(encodestr + serverIp).toLowerCase();
        String url = interfaceConfig.getConfig("experiences_categorydataword");
        String urlParams = "usercode=" + reqUserCode + "&usertoken=" + usertoken
                + "&querykey=" + querykey + "&ishs=" + ishs + "&startnum="
                + start + "&searchcount=" + Constants.PAGE_LIMIT;
        Map<String, Object> reqRet = PubFun.requestPostInterface(url, null,
            urlParams, Map.class);

        String errorCode = (String) reqRet.get("errorCode");
        if (StringUtil.isNotEmpty(errorCode)) {
            PubFun.renderErrorMsg(errorCode);
        }
        ret.put("returnCategoryDataWord",
            reqRet.get("returnCategoryDataWord") == null ? new String[] {}
                    : reqRet.get("returnCategoryDataWord"));

        // 查询相同关键字和相同hscode
        url = interfaceConfig.getConfig("experiences_groupquery");
        urlParams = "usercode=" + reqUserCode + "&usertoken=" + usertoken
                + "&querykey=" + querykey + "&ishs=" + ishs + "&startnum="
                + start + "&searchcount=" + Constants.PAGE_LIMIT;
        Map<String, Object> reqGroupRet = PubFun.requestPostInterface(url, null,
            urlParams, Map.class);
        if (reqGroupRet != null) {
            ret.put("gltReturnGroupHS",
                reqGroupRet.get("gltReturnGroupHS") == null ? new String[] {}
                        : reqGroupRet.get("gltReturnGroupHS"));
            ret.put("gltReturnGroupKey",
                reqGroupRet.get("gltReturnGroupKey") == null ? new String[] {}
                        : reqGroupRet.get("gltReturnGroupKey"));
        } else {
            ret.put("gltReturnGroupHS", new String[] {});
            ret.put("gltReturnGroupKey", new String[] {});
        }
        return ret;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> categoryData(String params) throws Exception {
        Map<String, Object> ret = new HashMap<String, Object>();

        // 解析参数
        JSONObject obj = JSONObject.parseObject(params);
        String ids = obj.getString("ids");
        if (StringUtil.isEmpty(ids)) {
            PubFun.renderErrorMsg("查询条件不能为空");
        }
        Integer start = obj.getInteger("start");
        if (start == null) {
            start = 0;
        }
        obj.put("usercode", reqUserCode);
        String usertoken = MD5Util.Md5(encodestr + serverIp).toLowerCase();
        obj.put("usertoken", usertoken);

        String url = interfaceConfig.getConfig("experiences_categorydata");
        String urlParams = "usercode=" + reqUserCode + "&usertoken=" + usertoken
                + "&ids=" + ids + "&startnum=" + start + "&searchcount="
                + Constants.PAGE_LIMIT;
        Map<String, Object> reqRet = PubFun.requestPostInterface(url, null,
            urlParams, Map.class);

        String errorCode = (String) reqRet.get("errorCode");
        if (StringUtil.isNotEmpty(errorCode)) {
            PubFun.renderErrorMsg(errorCode);
        }
        ret.put("returnCategoryData",
            reqRet.get("returnCategoryData") == null ? new String[] {}
                    : reqRet.get("returnCategoryData"));
        return ret;
    }

}
